package com.citusdata.elven.datastore;

import java.util.Comparator;


// Helper class that implements sort order for data store keys
public class DatastoreKeyComparator implements Comparator<byte[]> {

  public DatastoreKeyComparator() { }

  public int compare(byte[] first, byte[] second) {
    DatastoreKey firstKey  = new DatastoreKey(first);
    DatastoreKey secondKey = new DatastoreKey(second);

    String firstPartition  = firstKey.getPartitionKey();
    String secondPartition = secondKey.getPartitionKey();
    
    int partitionDiff = firstPartition.compareTo(secondPartition);
    if (partitionDiff != 0) {
      return partitionDiff;
    }

    long firstTimestamp  = firstKey.getTimestamp();
    long secondTimestamp = secondKey.getTimestamp();

    int timestampDiff = (int) (firstTimestamp - secondTimestamp);
    return timestampDiff;
  }
}
